//
//  HomeViewController.m
//  HospitalSystem
//
//  Created by jss on 16/5/4.
//
//

#import "HomeViewController.h"
#import "CZBtnModel.h"
#import "CZZLViewController.h"
#import "CycleScrollView.h"
#import "UIImageView+WebCache.h"

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface HomeViewController ()

//ScrollView
@property (weak, nonatomic) IBOutlet UIView *topView;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"nav"]];
    
    //轮播器
    [self loadCircleScrollViewImage];
    
    [self setupUI];
}

//设置界面
-(void)setupUI{
    
    //接收通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveDidClickBtn:) name:@"didBtn" object:nil];
    
   
}

//请求滚动scrollview图片
- (void)loadCircleScrollViewImage{
    
    NSArray *imagesArray = @[@"http://7xsiiq.com2.z0.glb.clouddn.com/h1.png",
                             @"http://7xsiiq.com2.z0.glb.clouddn.com/h2.png",
                             @"http://7xsiiq.com2.z0.glb.clouddn.com/h3.png"];
    
    NSMutableArray *imageViewArray = [NSMutableArray array];
    
    for (int i = 0; i < imagesArray.count; ++i) {
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.topView.frame.size.height)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:imagesArray[i]] placeholderImage:[UIImage imageNamed:@"good_image_default"] options:SDWebImageRetryFailed];
        
        [imageViewArray addObject:imageView];
    }
    
    CycleScrollView *cycleScorllView = [[CycleScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.topView.frame.size.height) animationDuration:4];
    
    cycleScorllView.backgroundColor = [[UIColor purpleColor] colorWithAlphaComponent:0.1];
    
    cycleScorllView.scrollView.showsHorizontalScrollIndicator = NO;
    
    [self.topView addSubview:cycleScorllView];
    
    cycleScorllView.totalPagesCount = ^NSInteger(void){
        return imageViewArray.count;
    };
    cycleScorllView.fetchContentViewAtIndex = ^UIView *(NSInteger pageIndex){
        
        return imageViewArray[pageIndex];
    };
    
    //因为原型图没有给出点击做什么事，但是呢，我们还是要留下接口的，因为一般的轮播器点击都会有交互的
    cycleScorllView.TapActionBlock = ^(NSInteger pageIndex){
        
        
        NSLog(@"点击了第%ld张",pageIndex);
        
    };
    
}

-(void)receiveDidClickBtn:(NSNotification *)info{
    
   CZBtnModel *model  = info.userInfo[@"model"];
    
 
    NSString *str = model.targetVC;
    
    if (str) {
        
        //获取要跳转的目标控制器
        Class targertVC = NSClassFromString(str);
        
        //根据目标控制器类型创建控制器对象
        UIViewController *targertCtr = [[targertVC alloc]init];
        
        targertCtr.view.backgroundColor = [UIColor orangeColor];
        
        targertCtr.title = model.title;
        
        [self.navigationController pushViewController:targertCtr animated:YES];
    }
    
    
    
    
    
    
}

//移除通知
-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
